package com.lex.main;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.httpclient.HttpException;
import org.apache.log4j.Logger;

import com.lex.thread.Spider;


/*
 * TODO complete the class documentation
 * @version $Revision:$
 */
public class Main {
  private static final String DESTINATION_KEY_PREFIX = "destination";
  private static final String CONFIG_PROPERTIES_FILE_NAME = "config.properties";
  private static PropertiesConfiguration config;
  
  private static Logger LOG = Logger.getLogger(Main.class);
  
  /**
   * TODO complete method documentation
   * 
   * @param args
   * @throws Exception
   * @throws HttpException
   */
  @SuppressWarnings("unchecked")
  public static void main(final String[] args) throws Exception {
    
    init();
    List<Thread> threadList = new ArrayList<Thread>();
    for (Iterator<String> iter = config.getKeys(DESTINATION_KEY_PREFIX); iter.hasNext();) {
      String key = iter.next();
      String url = config.getString(key);
      Spider s1 = new Spider(url);
      Thread t1 = new Thread(s1);
      threadList.add(t1);
    }
    LOG.info("Created threads: " + threadList.size());
     
    for (Thread thread : threadList) {
      thread.start();
    }
  }

  private static void init() throws ConfigurationException {
    config = new PropertiesConfiguration("cfg/"
        + CONFIG_PROPERTIES_FILE_NAME);
  }

}
